const tronWeb = require('./tronweb')
const config = require('./config')

const CID = config.contractId

async function getCurrentBlock() {
  const b = await tronWeb.trx.getCurrentBlock()
  console.log(b)
}

async function getContractTransactions() {
  const address = tronWeb.address.fromHex(CID)
  console.log('contract address', address)
  const result = await tronWeb.trx.getTransactionsToAddress(address, 10, 0)
  console.log(result)
}

async function getMyTransactions() {
  const trs = await tronWeb.trx.getTransactionsFromAddress("TTH6SAXLjxgfNnY7Ymrnvh1rPGZPz2tq85", 30, 0)
  console.log(JSON.stringify(trs, null, 2))
}

async function buyEnergy() {
  const addr = '41c244d064906c4b1e6c4ab3b719a14c0923c4fa58'
  const unsignedTrs = await tronWeb.transactionBuilder.freezeBalance(tronWeb.toSun(5000), 3, "ENERGY", addr)
  const trs = await tronWeb.trx.sign(unsignedTrs)
  const result = await tronWeb.trx.sendRawTransaction(trs)
  console.log('buyEnergy result:', result)
}

async function buyBandwidth() {
  const unsignedTrs = await tronWeb.transactionBuilder.freezeBalance(tronWeb.toSun(1000), 3, "BANDWIDTH")
  const trs = await tronWeb.trx.sign(unsignedTrs)
  const result = await tronWeb.trx.sendRawTransaction(trs)
  console.log('buyBandwidth result:', result)
}

async function getAccount() {
  const addr = '41c3c31e79ad394a837ce2d308aed7b7869375e647'
  const account = await tronWeb.trx.getAccountResources()
  console.log('account resource:', account)
  console.log(await tronWeb.trx.getAccount())
}

async function getContract() {
  const c = await tronWeb.contract().at(CID)
  console.log(c)
}

async function buyPixels() {
  const c = await tronWeb.contract().at(CID)
  const args = {
    shouldPollResponse: true,
    callValue: 20000000
  }
  const pixels = [
    500500, 0
  ]
  const result = await c.buyPixels(pixels).send(args)
  console.log('buyPixels result', result)
}

async function invokeContract() {
  const c = await tronWeb.contract().at(CID)
  const args = {
    shouldPollResponse: true,
    callValue: 2000000
  }
  const result = await c.test([135]).send(args)
  console.log('invoke result', result)
}

async function teamWithdrawal() {
  const c = await tronWeb.contract().at(CID)
  const args = {
    shouldPollResponse: true,
  }
  const result = await c.teamWithdraw().send(args)
  console.log('invoke result', result)
}

async function queryContract() {
  const c = await tronWeb.contract().at(CID)
  let result = await c.getPixel(498943).call()
	console.log('get pixel', result)
	result = await c.getUserInfo().call()
  for (let i in result) {
    result[i] = result[i].toString()
  }
  console.log('get userinfo', result)
	result = await c.getGameInfo().call()
  console.log('get game info', result)
  for (let r of result) {
    console.log(r.toString())
  }
  result = await c.teamBalance().call()
  console.log('team balance', result.toString())
}

async function getEvents() {
  let offset = 0
  let count = 0
  while (true) {
    let result = await tronWeb.getEventResult(CID, offset, 'Pot')
    console.log('pulled %d events', result.length)
    count += result.length
    if (result.length === 0) {
      break
    } else {
      offset = result[result.length - 1].timestamp + 1
    }
  }
  console.log('total %d events', count)
}

async function watch() {
  const c = await tronWeb.contract().at(CID)
  let count = 0
  c.Pot().watch((error, event) => {
    console.log('watch event', error, event)
    console.log('total count:', ++count)
  })
}

async function main() {
  console.time('time usage')
  // console.log(await tronWeb.isConnected())
  // await invokeContract()
  // await buyPixels()
  // await buyEnergy()
  // await buyBandwidth()
  await teamWithdrawal()
  await getAccount()
  // await getMyTransactions()
  // await getContract()
  // await queryContract()
  // await getEvents()
  // await watch()
  console.timeEnd('time usage')
}

main().then().catch(console.error)